/*
 * Copyright 2024 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef PYTHONUTIL_H
#define PYTHONUTIL_H

#include <Python.h>

#include <string>
#include <vector>
#include <map>
#include <variant>

#define DATA_MANAGEMENT_PYTHON_PATH "/usr/share/kylin-ai-business-framework/utils/python/"

namespace pythonutil {

std::map<std::string, std::vector<int64_t>> convertPythonDictToStdMap(PyObject* dictObject);
std::vector<std::string> convertPythonStringListToStd(PyObject* listObject);
std::string convertPythonStringToStd(PyObject* strObject);
int convertPythonIntToStd(PyObject* intObject);

PyObject* convertStringVectorToPyObject(const std::vector<std::string>& vec);

PyObject* convertDoubleVectorToPyObject(const std::vector<double>& vec);
PyObject* convertDoubleMatrixToPyObject(const std::vector<std::vector<double>>& matrix);

PyObject* convertVariantToPyObject(const std::variant<std::string, int64_t>& var);
PyObject* convertMapVariantToPyObject(const std::map<std::string, std::variant<std::string, int64_t>>& map);
PyObject* convertMapListToPyObject(const std::vector<std::map<std::string, std::variant<std::string, int64_t>>>& mapList);

std::vector<std::vector<double>> convertPythonListToMatrix(PyObject* pyMatrix);
std::vector<double> extractDoubles(PyObject* inner_list);

std::vector<std::map<std::string, std::variant<std::string, int64_t>>> convertPythonListToMapOfVariant(PyObject* pyList);
std::map<std::string, std::variant<std::string, int64_t>> convertPyDictToStdMap(PyObject* dict);
std::string convertPyUnicodeToStdString(PyObject* unicode);
std::variant<std::string, int64_t> convertPyVarToStd(PyObject* pyValue);

std::vector<float> convertPythonListToVectorFloat(PyObject* obj);

}

class PythonThreadLocker
{
public:
    PythonThreadLocker() : state(PyGILState_Ensure())
    {}
    ~PythonThreadLocker() {
        PyGILState_Release(state);
    }

private:
    PyGILState_STATE state;
};

#endif // PYTHONUTIL_H
